﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sx.Mall.Domain;
using Sx.Mall.WebContract.Dto;

namespace Sx.Mall.WebService.Common
{
    public static class DtoMapper
    {

        static DtoMapper()
        {
            AutoMapper.Mapper.CreateMap<MallUserDto, MallUser>();
            AutoMapper.Mapper.CreateMap<MallUser, MallUserDto>();

            AutoMapper.Mapper.CreateMap<MallManagerDto, MallManager>();
            AutoMapper.Mapper.CreateMap<MallManager, MallManagerDto>();

        }

        /// <summary>
        /// S转换为T
        /// </summary>
        /// <typeparam name="S">本来类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="source">实体</param>
        /// <returns></returns>
        public static T Convert<S, T>(this S source)
            where S : class
            where T : class
        {
            return AutoMapper.Mapper.Map<S, T>(source);
        }

        /// <summary>
        /// List<S>转换List<T>
        /// </summary>
        /// <typeparam name="S">本来类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="source">列表</param>
        /// <returns></returns>
        public static List<T> Convert<S, T>(this List<S> source)
            where S : class
            where T : class
        {
            return AutoMapper.Mapper.Map<List<S>, List<T>>(source);
        }


        /// <summary>
        /// 实体赋值（将T里面的内容映射到S上）
        /// </summary>
        /// <typeparam name="T">目标</typeparam>
        /// <typeparam name="S">目的类型</typeparam>
        /// <param name="old"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static S Map<T, S>(this T old, S source)
            where S : class
            where T : class
        {
            return AutoMapper.Mapper.Map<T, S>(old, source);
        }


    }
}
